package com.hotel.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.hotel.entidade.Categoria;

public class CategoriaDAO {

	private Session session;
	
	public CategoriaDAO(Session session) {
		this.session = session;
	}

	public void insere(Categoria categoria) {
		session.save(categoria);
		session.beginTransaction().commit();
		session.close();
	}

	public void deleta(Categoria categoria) {
		session.delete(categoria);
		session.beginTransaction().commit();
	}

	public void atualisa(Categoria categoria) {
		session.update(categoria);
		session.beginTransaction().commit();
	}

	public Categoria buscaPorId(int id) {
		return (Categoria) session.load(Categoria.class, id);
	}
	
	public List<Categoria> pesquisarCategoria(String keyword){
		@SuppressWarnings("unchecked")
		List<Categoria> list = session.createCriteria(Categoria.class)
			.add(Restrictions.ilike("categoria", keyword, MatchMode.ANYWHERE)).addOrder(Order.asc("categoria"))
            .list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Categoria> listaTodos() {
		return session.createQuery("FROM Categoria ORDER BY categoria ASC").list();
		// return session.createCriteria(Produto.class).list();
	}

}
